<?php

namespace app\api\controller;
use app\common\model\Message as Msg;
use app\common\model\Post as P;
use think\facade\Db;

class Message extends ApiBase {

    // 不需要验证token的方法
    protected $middleware = [
        'app\api\middleware\Auth' => ['except' => ["num"]],
    ];

	public function list() {
		$msgType = input("type");
		$uid = $this->uid;
		$where[] = ["message.status", "in", [0, 1]];

		if ($msgType == 1) {
			$where[] = ["to_uid", "=", $uid];
			$where[] = ["message.type", "in", [1, 3]];
		}

		if ($msgType == 3) {
			$where[] = ["to_uid", "=", $uid];
			$where[] = ["message.type", "=", 2];
		}
		if ($msgType == 5) {
			$where[] = ["message.type", "=", 5];
			$list = Msg::withJoin(["userInfo" => ["username", "avatar"]])->order("m_id desc")->where($where)->select();
		} else if ($msgType == 6) {
			$where[] = ["message.type", "=", 6];
			$list = Msg::withJoin(["userInfo" => ["username", "avatar"]])->order("m_id desc")->where($where)->select();
		} else if ($msgType != 2) {
			$list = Msg::withJoin(["userInfo" => ["username", "avatar"], "postInfo"])->order("m_id desc")->where($where)->select();
		} else {
			$where[] = ["to_uid", "=", $uid];
			$where[] = ["message.type", "=", 4];
			$list = Msg::withJoin(["userInfo" => ["username", "avatar"]])->order("m_id desc")->where($where)->select();
		}
		return success($list);
	}

	//所有消息列表
	public function allList() {
		$list = Msg::withJoin(["userInfo"])->paginate(10);
		return success($list);
	}
	public function status() {
		$msgType = input("type");

		$uid = $this->uid;

		$where[] = ["to_uid", "=", $uid];

		if ($msgType == 1) {
			$where[] = ["type", "in", [1, 3]];
		}

		if ($msgType == 2) {
			$where[] = ["type", "=", 4];
		}

		if ($msgType == 3) {
			$where[] = ["type", "=", 2];
		}
		if ($msgType == 5) {
			$where[] = ["type", "=", 5];
		}
		if ($msgType == 6) {
			$where[] = ["type", "=", 6];
		}

		$res = Db::name("message")->where($where)->update(["status" => 1]);

		if ($res) {
			return notice("消息状态修改成功");
		}
		return error("消息状态修改失败");
	}

    //推送公众号文章消息
    public function sendArticle() {
        $title = input("title");
        $uids = input("uids");
        $content = input("content");

        $uidArr = explode(',',$uids);


        if(empty($uids)){
            $uidArr = Db::name("user")->where("type",0)->column("uid");
        }

        foreach ($uidArr as $item){
            $this->sendMsg(2,$item,"",5,$content,$title);
        }

        return notice("消息推送成功");
    }
	public function num() {
		$uid = $this->uid;
		$where["status"] = 0;

		$thumbCollect = Db::name("message")->where("to_uid", $uid)->where($where)->where("type", "in", [1, 3])->count();
		$follow = Db::name("message")->where("to_uid", $uid)->where($where)->where("type", 4)->count();
		$comment = Db::name("message")->where("to_uid", $uid)->where($where)->where("type", 2)->count();
		$allCount = Db::name("message")->where("to_uid", $uid)->where($where)->count();

		//推送文章消息
		$article_msg_list = Db::name("message")->where("to_uid", $uid)->where("type", 5)->order("m_id desc")->select()->each(function ($item){
		    $item["article_title"] = Db::name("post")->where("id",$item["post_id"])->value("title");
		    $item["user_info"] = Db::name("user")->where("uid",$item["from_uid"])->find();
		    return $item;
        });

		//私信
        $chatUids = Db::name("message")->where("to_uid", $uid)->where("type", 6)->order("m_id desc")->column("from_uid");

        $chatUids = array_unique($chatUids);

        $chatMsgList = [];
        $where1["to_uid"] = $uid;
        foreach ($chatUids as $key => $id){
            $where1["from_uid"] = $id;

            $msg["count"] = Db::name("message")->where($where1)->where("type", 6)->where("status",0)->count();
            $msg["msg"] = Db::name("message")->where($where1)->order("m_id desc")->limit(1)->find();
            $msg["user_info"] = Db::name("user")->where("uid",$id)->find();

            array_push($chatMsgList,$msg);
        }

		$data = [
			"thumb_collect" => $thumbCollect,
			"follow" => $follow,
			"comment" => $comment,
            "all_count" => $allCount,
			"article_msg_list" =>$article_msg_list,
            "chat_msg_list" =>$chatMsgList
		];

		return success($data);
	}

	//删除图文消息
	public function delMsg() {
		$id = input("id", "", "intval");
		$res = Msg::destroy($id);

		if ($res) {
			return notice("删除成功");
		}
		return error("删除失败");
	}

    //删除私信消息
    public function delChat(){
	    $uid = input("uid");

	    $where["from_uid"] = $uid;
	    $where["to_uid"] = $this->uid;

	    $res = Msg::where($where)->delete();

        if ($res) {
            return notice("删除成功");
        }
        
        return error("删除失败");
    }

    //图文消息更改阅读状态
    public function articleMsgState(){
        $id = input("post_id");
       Db::name("message")->where("m_id",$id)->update(["status" => 1]);
    }
    //更新私信阅读状态
    public function updateChatStatus(){
	    $where["from_uid"] = input("uid");
	    $where["to_uid"] = $this->uid;

        Db::name("message")->where($where)->update(["status" => 1]);
    }
}